﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using HandyControl.Data;
using Protocal.App.Common;
using 多协议通信平台.Common;

namespace 多协议通信平台.ViewModels.Login
{
    public partial class UserRegisterViewModel : ObservableObject,IDataErrorInfo
    {
        [ObservableProperty]
        private string avatar;

        [ObservableProperty]
        private string username;

        [ObservableProperty]
        private string account;

        [ObservableProperty]
        private string phone;

        [ObservableProperty]
        private string email;

        [ObservableProperty]
        private string password;

        [ObservableProperty]
        private string password2;

        private int count = 0;

        public string this[string columnName]
        {
            get
            {
                if(count < 6)
                {
                    count++;
                    return "";
                }
                Error = "";
                CheckResult checkResult = new CheckResult();
                switch (columnName)
                {
                    case "Username":
                        checkResult = ValidationHelper.Validate("Username", Username);
                        break;
                    case "Account":
                        checkResult = ValidationHelper.Validate("Account", Account);
                        break;
                    case "Phone":
                        checkResult = ValidationHelper.Validate("Phone", Phone);
                        break;
                    case "Email":
                        checkResult = ValidationHelper.Validate("Email", Email);
                        break;
                    case "Password":
                        checkResult = ValidationHelper.Validate("Password", Password);
                        break;
                    case "Password2":
                        checkResult = ValidationHelper.Validate("Password", Password2);
                        if (checkResult.State)
                        {
                            if(Password != Password2)
                            {
                                checkResult.State = false;
                                checkResult.Msg = "两次输入的密码不一致";
                            }
                        }
                        break;
                    default:
                        checkResult.State = true;
                        break;
                }
                if (!checkResult.State)
                {
                    Error = checkResult.Msg;
                }
                return Error;
            }
        }

        public string Error { get; set; }

        [RelayCommand]
        public void SelectImg(RoutedEventArgs arg)
        {
            ImageSelector? selector = arg.Source as ImageSelector;
            Avatar = selector.Uri.LocalPath;
        }

        [RelayCommand]
        public void UnSelectImg(RoutedEventArgs arg)
        {
            Avatar = "";
        }

        [RelayCommand]
        public void DoRegister()
        {
            if (string.IsNullOrEmpty(Error))
            {
                Growl.Error(new GrowlInfo()
                {
                    Message = Error,
                    WaitTime = 2,
                    ShowCloseButton = true,
                });
                return;
            }


        }
    }
}
